#include <stdio.h>
#include <stdbool.h>

//初始化
void InitBoard(char (*Board)[3])
{
    for(int i = 0; i < 3;i++)
    {
        for(int j = 0; j < 3; j++)
        {
            Board[i][j] = ' ';
        }
    }
}

//打印棋盘
void PrintBoard(char (*Board)[3])
{
   for(int i = 0 ; i < 3; i++ )
   {
      for(int j = 0; j < 3; j++)
      {
        printf(" %c ",Board[i][j]);
        if( j < 2 )
            printf("|");
      }
      printf("\n");
      if( i < 2 )
          printf("---|---|---\n");
   }
}

//下棋
bool  MakeMove(char (*Board)[3],int row,int colum,char player)
 {
    if(row < 0 || row >=3 || colum < 0 || colum >= 3)
    {
        printf("下的位置是非法位置，请重新输入！");
        return false;
    }
    if(Board[row][colum] != ' ')
    {
        printf("当前位置已经有棋子了，请重新输入！\n");
        return false;
    }
     Board[row][colum] = player;
     return true;
 }
 //判断是否胜利
 bool CheckIsWin(char(*Board)[3],char player)
 {
     for(int i = 0; i < 3;i++)
     {
        int count = 0;
        while (Board[i][count] == player && count < 3)
        {
            count++;
        }
        if(count == 3)
        {
          return true;
        }
    }
     for(int j = 0;j < 3;j++)
     {
        int count = 0;
        while (Board[count][j] == player && count < 3)
        {
            count++;
        }
        if(count == 3)
        {
          return true;
        }
    }
if(Board[0][0] == player && Board[1][1] == player && Board[2][2] == player)
        return true;
if(Board[0][2] == player && Board[1][1] == player && Board[2][0] == player)
        return true;
return false;
 }
 //判断是否是平局
 bool CheckIsDraw(char (*Board)[3],char player)
 {
    for(int i = 0 ; i< 3;i++)
    { 
        for(int j = 0;j<3;j++)
        {
            if(Board[i][j] == ' ')
                return false;
        }
    }
    return true;//平局
 }
int main()
{
   char Board[3][3] = {0};
   InitBoard(Board);
   //PrintBoard(Board);

   char player = 'x';
   while (1)
   {
     PrintBoard(Board);
     int row = 0;
     int colum = 0;
     printf("请玩家|%c|下棋:",player);
     //从终端读取输入
     scanf("%d %d",&row,&colum);
     if(MakeMove(Board,row - 1,colum - 1,player) == false)
     {
         continue;
     }
     if(CheckIsWin(Board,player) == true)
     {
        PrintBoard(Board);
        printf("恭喜|%c|玩家获胜！\n",player);
        break;
     }
     if(CheckIsDraw(Board,player) == true)
     {
        PrintBoard(Board);
        printf("平局！\n");
        break;
     }
     player = (player == 'x') ? 'o' : 'x';
   }
   return 0;
}